FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})

FIND_PACKAGE(Java REQUIRED) # javac, jar
FIND_PACKAGE(JNI REQUIRED)

INCLUDE_DIRECTORIES(
		${JAVA_INCLUDE_PATH}
		${JAVA_INCLUDE_PATH2}
	)

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/bindings)

set(SWIG_MODULE_biosignal_EXTRA_DEPS
${CMAKE_SOURCE_DIR}/bindings/biosignal_common.i)


SET(BIOSIGNAL_PACKAGE biosignal)
SET(BIOSIGNAL_JAVA_DIR ${BIOSIGNAL_PACKAGE})
SET(CMAKE_SWIG_FLAGS -DSWIG_JAVA -package ${BIOSIGNAL_JAVA_DIR} )
SET(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_BINARY_DIR}/${BIOSIGNAL_JAVA_DIR})
FILE(MAKE_DIRECTORY ${CMAKE_SWIG_OUTDIR})




SET_SOURCE_FILES_PROPERTIES(biosignal.i PROPERTIES CPLUSPLUS ON)
SWIG_ADD_MODULE(biosignalJava java biosignal.i )
SWIG_LINK_LIBRARIES(biosignalJava ${JAVA_JVM_LIBRARY} biosignal ) 


## The library name expected by the JNI relative to the system:
##   MacOS  : libLIBRARY.jnilib
##   Windows: LIBRARY.dll
##   Linux  : libLIBRARY.so
##
## where LIBRARY is used in java to load the library:
##      System.loadLibrary("LIBRARY");
IF(APPLE)
  SET_TARGET_PROPERTIES(${SWIG_MODULE_biosignalJava_REAL_NAME} PROPERTIES
    PREFIX "lib" SUFFIX ".jnilib")
ELSEIF(WIN32)
  SET_TARGET_PROPERTIES(${SWIG_MODULE_biosignalJava_REAL_NAME} PROPERTIES
    PREFIX "" SUFFIX ".dll")
ELSEIF(UNIX)
  SET_TARGET_PROPERTIES(${SWIG_MODULE_biosignalJava_REAL_NAME} PROPERTIES
    PREFIX "lib" SUFFIX ".so")
ENDIF(APPLE)




ADD_CUSTOM_COMMAND (
    OUTPUT biosignal.jar
    COMMAND ${JAVA_COMPILE} ARGS -source 1.5 "${CMAKE_SWIG_OUTDIR}/*.java"
    COMMAND ${JAVA_ARCHIVE} ARGS cvf ${CMAKE_CURRENT_BINARY_DIR}/biosignal.jar "${BIOSIGNAL_JAVA_DIR}/*.class"
    DEPENDS "${swig_generated_file_fullname}"
    COMMENT "javac *.java; jar cvf -> biosignal.jar"
)

ADD_CUSTOM_TARGET(BioSignalJavaJar ALL
    DEPENDS biosignal.jar
    COMMENT "building biosignal.jar"
)


INCLUDE(UseJavaTest)

SET_SOURCE_FILES_PROPERTIES(TestJava1.java PROPERTIES RUNTIMEPATH ${CMAKE_CURRENT_BINARY_DIR}/biosignal.jar)
ADD_JAVA_TEST(test_java1 TestJava1.java)

INSTALL(TARGETS biosignalJava DESTINATION ${JAVA_JNI_DIR} EXPORT biosignal-java-targets)
INSTALL(FILES  ${CMAKE_CURRENT_BINARY_DIR}/biosignal.jar DESTINATION ${JAVA_JAR_DIR})
INSTALL(EXPORT biosignal-java-targets DESTINATION ${BIOSIGNAL_INSTALL_LIB_DIR}/java)  

